<template>
  <div
    class='xtx-goods-page'
    v-if="goods"
  >
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[2].id}`">{{goods.categories[2].name}}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[1].id}`">{{goods.categories[1].name}}</XtxBreadItem>
        <XtxBreadItem to="/">{{goods.name}}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :goods='goods.mainPictures' />
          <GoodsSales />
        </div>
        <div class="spec">
          <GoodsName :goods="goods" />
          <GoodsSku
            @change="changeSku"
            :goods="goods"
          />
          <XtxNumbox
            label="数量"
            v-model="num"
            :max="goods.inventory"
          />
          <XtxButton
            type="primary"
            style="margin-top:20px;"
          >加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goods.id" />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs :goods="goods" />
          <!-- 注意事项 -->
          <GoodsWarn />
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot />
          <GoodsHot :type="2" />
        </div>
      </div>
    </div>
  </div>
  <div
    v-else
    class="loading"
  ></div>
</template>

<script>
import GoodsSales from './components/goods-sales'
import GoodsName from './components/goods-name'
import GoodsRelevant from './components/goods-relevant'
import GoodsSku from './components/goods-sku'
import GoodsTabs from './components/goods-tabs'
import GoodsHot from './components/goods-hot'
import GoodsWarn from './components/goods-warn'
import { findGoods } from '@/api/goods'
import { useRoute } from 'vue-router'
import { provide, ref } from 'vue'
import GoodsImage from './components/goods-image'
const uesGoodsData = () => {
  const route = useRoute()
  const goods = ref(null)
  if (route.params.id) {
    findGoods('1369155859933827074').then(({ result }) => {
      goods.value = result
    })
  }
  return { goods }
}
export default {
  name: 'XtxGoodsPage',
  components: { GoodsRelevant, GoodsImage, GoodsSales, GoodsName, GoodsSku, GoodsTabs, GoodsHot, GoodsWarn },
  setup () {
    const { goods } = uesGoodsData()
    const num = ref(1)
    // sku改变时候触发
    const changeSku = (sku) => {
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
      }
    }
    provide('goods', goods)

    return { goods, changeSku, num }
  }
}
</script>

<style scoped lang='less'>
.loading {
  margin-top: 72px;
  height: 600px;
  background: #fff url(../../assets/images/loading.gif) no-repeat center;
}
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}

.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
